package com.cheney.common.sensitive.properties;

import ch.qos.logback.classic.spi.ILoggingEvent;
import com.cheney.common.sensitive.handler.DefaultStringMessageHandler;
import com.cheney.common.sensitive.handler.LogbackMessageHandler;
import com.cheney.common.sensitive.strategy.SensitiveStrategy;

import java.util.*;

/**
 * 脱敏配置项
 */
public class DefaultStringProperties {

    /**
     * 是否兼容反斜杠
     * <p>
     * 举例说明：
     * <br/> 开启前: 能识别{"k":"v"}，但不能识别{\"k\":\"v\"}
     * <br/> 开启后: 能识别{"k":"v"}，也能识别{\"k\":\"v\"}
     * </p>
     */
    private boolean compatBackslash = true;

    /**
     * 脱敏的策略 以及 对应的脱敏字段集合
     */
    private Map<SensitiveStrategy, Set<String>> strategies;

    /**
     * key-value 之间的分隔符
     */
    private Set<DefaultStringMessageHandler.KeyValueDelimiter> keyValueDelimiter = newSet(
            new DefaultStringMessageHandler.KeyValueDelimiter[]{
                    DefaultStringMessageHandler.KeyValueDelimiter.COLON,
                    DefaultStringMessageHandler.KeyValueDelimiter.EQUAL,
                    DefaultStringMessageHandler.KeyValueDelimiter.ARROW
            }
    );

    /**
     * 指定短路插件
     * <p>
     * 当shortCircuitPlugins中存在{@link LogbackMessageHandler#support(ILoggingEvent)}匹配的插件时，
     * 使用该插件进行脱敏，而不用脱敏器进行脱敏
     * </p>
     */
    private List<LogbackMessageHandler> shortCircuitPlugins = new ArrayList<>();

    /**
     * 创建set
     */
    public static <T> Set<T> newSet(T[] delimiters) {
        if (delimiters == null) {
            return new HashSet<>();
        }
        return new HashSet<>(Arrays.asList(delimiters));
    }

    public boolean isCompatBackslash() {
        return compatBackslash;
    }

    public void setCompatBackslash(boolean compatBackslash) {
        this.compatBackslash = compatBackslash;
    }

    public Map<SensitiveStrategy, Set<String>> getStrategies() {
        return strategies;
    }

    public void setStrategies(Map<SensitiveStrategy, Set<String>> strategies) {
        this.strategies = strategies;
    }

    public List<LogbackMessageHandler> getShortCircuitPlugins() {
        return shortCircuitPlugins;
    }

    public void setShortCircuitPlugins(List<LogbackMessageHandler> shortCircuitPlugins) {
        this.shortCircuitPlugins = shortCircuitPlugins;
    }

    public Set<DefaultStringMessageHandler.KeyValueDelimiter> getKeyValueDelimiter() {
        return keyValueDelimiter;
    }

    public void setKeyValueDelimiter(Set<DefaultStringMessageHandler.KeyValueDelimiter> keyValueDelimiter) {
        this.keyValueDelimiter = keyValueDelimiter;
    }

    @Override
    public String toString() {
        return "DefaultStringProperties{" +
                "hitStringValueIsJson=" + compatBackslash +
                ", strategies=" + strategies +
                '}';
    }
}